<!-- 执行器管理 -->
<template>
  <div class="journal">
    <el-card class="!border-none" shadow="never">
      <el-form class="ls-form" :model="formData" inline>
        <el-form-item label="appname">
          <el-input
            class="w-[280px]"
            placeholder="请输入"
            v-model="formData.appname"
            clearable
            @keyup.enter="resetPage"
          />
        </el-form-item>

        <el-form-item label="名称">
          <el-input
            class="w-[280px]"
            placeholder="请输入"
            v-model="formData.title"
            clearable
            @keyup.enter="resetPage"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="!border-none mt-4" shadow="never" v-loading="pager.loading">
      <el-button type="primary" @click="handleAdd">新增</el-button>
      <div style="margin-top: 10px">
        <el-table :data="pager.lists" size="large">
          <!-- <el-table-column label="记录ID" prop="operId" /> -->
          <el-table-column label="appname" prop="appname" min-width="140" />
          <el-table-column prop="title" label="名称" min-width="100" />

          <el-table-column label="注册方式" prop="addressType" min-width="160">
            <template #default="{ row }">
              {{ row.addressType == 0 ? "自动注册" : "手动录入" }}
            </template>
          </el-table-column>
          <el-table-column
            label="OnLine 机器地址"
            prop="addressList"
            min-width="120"
            show-overflow-tooltip
          />
          <el-table-column label="操作" width="160" fixed="right">
            <template #default="{ row }">
              <el-button type="primary" link @click="handleEdit(row)">
                编辑
              </el-button>
              <el-button type="danger" link @click="handleDelete(row.id)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="flex mt-4 justify-end">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>
    <edit-popup
      v-if="showEdit"
      ref="editRef"
      @success="getLists"
      @close="showEdit = false"
    />
  </div>
</template>

<script lang="ts" setup name="post">
import { pageListJobGroup, delJobGroup } from "@/api/system/jobapi";
import { usePaging } from "@/layout/hooks/usePaging";
import feedback from "@/utils/feedback";
import EditPopup from "./edit.vue";
const editRef = shallowRef<InstanceType<typeof EditPopup>>();
const showEdit = ref(false);
const formData = reactive({
  appname: "",
  title: ""
});

const { pager, getLists, resetPage, resetParams } = usePaging({
  fetchFun: pageListJobGroup,
  params: formData
});

console.log("pager", pager);

const handleAdd = async () => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("add");
};

const handleEdit = async (data: any) => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("edit");
  editRef.value?.getDetail(data);
};

const handleDelete = async (id: number) => {
  await feedback.confirm("确定要删除？");
  await delJobGroup(id);
  feedback.msgSuccess("删除成功");
  getLists();
};

getLists();
</script>

<style lang="scss" scoped></style>
